﻿using Gengens.Disk.Entity;
using Gengens.Disk.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Gengens.Disk.Service
{
    public class BaseService<Model> : IBaseService<Model> where Model : BaseModel<Model>
    {
        private readonly BaseRepository<Model> _repository;
        public BaseService(BaseRepository<Model> repository)
        {
            _repository = repository;
        }

        #region 工具属性

        public AppDbCtx AppDbCtx => _repository.AppDbCtx;
        public DbSet<Model> Models => _repository.Models;
        public DatabaseFacade Database => _repository.Database;

        #endregion

        #region 仓库层的默认实现
        public virtual Model Find(Guid id) => _repository.Find(id);
        public virtual async Task<Model> FindAsync(Guid id) => await _repository.FindAsync(id);
        public virtual List<Model> FindAll() => _repository.FindAll();
        public virtual async Task<List<Model>> FindAllAsync() => await _repository.FindAllAsync();
        public virtual List<Model> Select(Func<Model, bool> whereFunc) => _repository.Select(whereFunc);
        public virtual async Task<List<Model>> SelectAsync(Func<Model, bool> whereFunc) => await _repository.SelectAsync(whereFunc);
        public virtual List<Model> PageSelect(int pageSize, int pageIndex, Func<Model, bool> whereFunc) => _repository.PageSelect(pageSize, pageIndex, whereFunc);
        public virtual async Task<List<Model>> PageSelectAsync(int pageSize, int pageIndex, Func<Model, bool> whereFunc) => await _repository.PageSelectAsync(pageSize, pageIndex, whereFunc);


        public virtual Model Insert(Model model) => _repository.Insert(model);
        public virtual Task<Model> InsertAsync(Model model) => _repository.InsertAsync(model);
        public virtual void InsertRange(List<Model> models) => _repository.InsertRange(models);
        public virtual Task InsertRangeAsync(List<Model> models) => _repository.InsertRangeAsync(models);

        public virtual void Update(Model model) => _repository.Update(model);
        public virtual void UpdateRange(List<Model> models) => _repository.UpdateRange(models);

        public virtual void Delete(Model model) => _repository.Delete(model);
        public virtual void DeleteRange(List<Model> models) => _repository.DeleteRange(models);

        public virtual void SaveChanges() => _repository.SaveChanges();
        public virtual async Task SaveChanegsAsync() => await _repository.SaveChangesAsync();

        public virtual async Task<int> ExecuteSqlRawAsync(string sql, params object[] args) => await _repository.ExecuteSqlRawAsync(sql, args);

        #endregion







    }
}
